Hệ thống quản lý phương tiện trong PHP
1 <?php
2
3
4 if(isset($_POST['submit'])){
5 $target_dir = "uploads/";
6 $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
7 $uploadOk = 1;
8 $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
9 // Check if image file is a actual image or fake image
10 if(isset($_POST["submit"])) {
11 $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
12 if($check !== false) {
13 echo "File is an image - " . $check["mime"] . ".";
14 $uploadOk = 1;
15 } else {
16 echo "File is not an image.";
17 $uploadOk = 0;
18 }
19 }
20 // Check if file already exists
21 if (file_exists($target_file)) {
22 echo "Sorry, file already exists.";
23 $uploadOk = 0;
24 }
25 // Check file size
26 if ($_FILES["fileToUpload"]["size"] > 500000) {
27 echo "Sorry, your file is too large.";
28 $uploadOk = 0;
29 }
30 // Allow certain file formats
31 if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
32 && $imageFileType != "gif" ) {
33 echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
34 $uploadOk = 0;
35 }
36 // Check if $uploadOk is set to 0 by an error
37 if ($uploadOk == 0) {
38 echo "Sorry, your file was not uploaded.";
39 // if everything is ok, try to upload file
40 } else {
41 if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
42 echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
43 } else {
44 echo "Sorry, there was an error uploading your file.";
45 }
46 }
47 }
48
49 ?>
50
51
52 <!DOCTYPE html>
53 <html>
54 <body>
55
56 <form action="" method="post" enctype="multipart/form-data">
57 Select image to upload:
58 <input type="file" name="fileToUpload" id="fileToUpload">
59 <input type="submit" value="Upload Image" name="submit">
60 </form>
61
62 </body>
63 </html